package com.ruoyi.yfdw.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.yfdw.domain.Dict;
import com.ruoyi.yfdw.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author : zll
 * @date : 2024/11/2 19:51/**
 * @BelongsProject: ruoyi
 * @BelongsPackage: com.ruoyi.yfdw.controller
 * @Description: TODO
 * @Author: zll
 * @CreateTime: 2024/11/02 19:51
 * @Version 1.0
 **/
@RestController
@RequestMapping("/report/yfdw/dict/type")
@Api(tags = "字典类型接口")
@Slf4j
public class DictController extends BaseController {

    @Autowired
    private IDictService iDictService;

    /**
     * @Description：根据入参查询字典类型列表
     * @Author: zll
     * @Date: 2024/11/4 20:02
     * @param dict
     * @param pageNum
     * @param pageSize
     * @return com.ruoyi.common.core.page.TableDataInfo
     **/
    @PutMapping("/list")
    @ApiOperation(value = "字典类型列表查询接口")
    public TableDataInfo list(@Param("dict") Dict dict, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize){
        //startPage();
        //logger.info("dict={}",dict);
        List<Dict> dicts = iDictService.getList(dict,pageNum,pageSize);
        //logger.info("dicts={}",dicts);
        return getDataTable(dicts);
    }

    @PutMapping("/add")
    @ApiOperation(value = "字典类型增加接口")
    public AjaxResult add(@RequestBody Dict dict){
        logger.info("add dict={}",dict);
        Integer i = iDictService.addDictType(dict);
        return success(i);
    }

    @GetMapping("/getDictTypeByDictGroupCodeAntDictCode/{dictGroupCode}/{dictCode}")
    @ApiOperation(value = "根据字典分组代码和字典代码查询字典类型")
    @Log(title = "查询字典类型",businessType = BusinessType.OTHER)
    public AjaxResult getDictTypeByDictGroupCodeAntDictCode(@PathVariable String dictGroupCode,@PathVariable String dictCode){
        //logger.info("dictList={}",dictList);
        Dict dict = new Dict();
        dict.setDictGroupCode(dictGroupCode);
        dict.setDictCode(dictCode);
        dict = iDictService.getDictTypeByDictGroupCodeAntDictCode(dict);
        logger.info("dict={}",dict);
        return success(dict);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改字典类型")
    @Log(title = "更新接口数据" ,businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody Dict dict){
        int i = 0;
        if(dict != null){
            i = iDictService.updateDictType(dict);
        }
        logger.info("i={}",i);
        if(i > 0){
            return AjaxResult.success("更新成功",i);
        }else {
            return AjaxResult.error(i,"error");
        }
    }

    @DeleteMapping("/delete/{dictGroupCode}/{dictCode}")
    @ApiOperation(value = "批量删除接口")
    public AjaxResult delete(@PathVariable String dictGroupCode,@PathVariable String dictCode){
        logger.info("dicts={}",dictGroupCode);
        Dict dict = new Dict();
        int i = 0;

        //如果只有一个类型入参，则设置单一对象
        if(!dictGroupCode.contains(String.valueOf(','))){
            dict.setDictGroupCode(dictGroupCode);
            dict.setDictCode(dictCode);
            i = iDictService.deleteDictType(dict);
        }else {
            //如果存在多个类型入参，将字符串转成数组，并循环遍历删除
            List<String> dictGroupCodeList = Arrays.asList(dictGroupCode.split(","));
            List<String> dictCodeList = Arrays.asList(dictCode.split(","));
            Iterator<String> iteratorDictGroupCode = dictGroupCodeList.iterator();
            Iterator<String> iteratorDictCode = dictCodeList.iterator();
            while(iteratorDictGroupCode.hasNext() && iteratorDictCode.hasNext()){
                dict.setDictGroupCode(iteratorDictGroupCode.next());
                dict.setDictCode(iteratorDictCode.next());
                logger.info("dict={}",dict);
                i = iDictService.deleteDictType(dict);
            }
        }
        //iDictService.deleteDictType()
        return AjaxResult.success(i);
    }


}

